package com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.FileEntity;
import com.slkj.shilixiaoyuanapp.entity.NoticeDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.code.FileCode;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.LookPicActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.FileDownAdapter;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import com.slkj.shilixiaoyuanapp.view.popwindow.topMenu.MenuItem;
import com.slkj.shilixiaoyuanapp.view.popwindow.topMenu.TopRightMenu;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 通知详情
 */
@ActivityInfo(layout = R.layout.activity_notice_detail,isShowMore = true,title = "通知详情", rightHint = "更多")
public class NoticeDetailActivity extends BaseActivity {
    @InjectParam
    int id;
    @InjectParam
    String title;
    public static final  int TYPE_HIS = 1;
    public static final  int TYPE_MESSAGE = 2;
    public static final  int TYPE_COLLECTION = 3;
    @InjectParam
    boolean isCanTx;
    @InjectParam
    int type;//进入的type，1：我发布的历史记录，2：首页消息，3：我的收藏
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.recycer_file)
    RecyclerView recycFile;
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    private TopRightMenu mTopRightMenu;
    FileDownAdapter fileAdapter;
    //已经下载过的文件
    List<String> hasFiles = new ArrayList<>();
    NoticeDetailEntity detailEntity;
    ArrayList<String> imgs=new ArrayList<>();
    @Override
    protected void init() {
        NoticeDetailActivity__JumpCenter.bind(this);
        if (TextUtils.isEmpty(title)){
            title = "通知详情" ;
        }
        setTitle(title);
        recycFile.setLayoutManager(new LinearLayoutManager(this));
        recycFile.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        HttpHeper.get().toolService()
                .getNoticeDetail(id)
                .compose(bindToLifecycle())
                .compose(getThread())
                .map(a -> {
                    File file = new File(FileCode.MY_FILE);
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (int i = 0; i < files.length; i++) {
                            hasFiles.add(files[i].getName());
                        }
                        for (FileEntity fileEntity : a.getData().getFiles()){
                            fileEntity.setHave(hasFiles.contains(fileEntity.getName()));
                        }
                    }
                    return a;
                })
                .subscribe(new CommonCallBack<NoticeDetailEntity>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(NoticeDetailEntity data) {
                        detailEntity = data;
                        tvContent.setText(data.getBody());
                        tvName.setText(data.getTitle());
                        tvTime.setText(data.getIntime());
                        if (data.getImages().size() == 0) {
                            img.setVisibility(View.GONE);
                        } else {
                            img.setVisibility(View.VISIBLE);
                            imgs.addAll(data.getImages());
                            Glide.with(NoticeDetailActivity.this).load(data.getImages().get(0)).into(img);
                            img.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    LookPicActivity__JumpCenter.builder(NoticeDetailActivity.this).setImgUrls(imgs)
                                            .setStartPos(0)
                                            .create()
                                            .go();
                                }
                            });
                        }
                        if (!data.getFiles().isEmpty()) {
                            fileAdapter = new FileDownAdapter(NoticeDetailActivity.this, data.getFiles());
                            recycFile.setAdapter(fileAdapter);
                        }
                    }

                });
    }


    @Override
    protected void onRightTitleClick() {
        if (detailEntity == null ){
            return;
        }
        mTopRightMenu = new TopRightMenu(this);
        List<MenuItem> menuItems = new ArrayList<>();

        menuItems.add(new MenuItem(detailEntity.isCollection()?R.drawable.ic_issc:R.drawable.ic_pop_sc, detailEntity.isCollection()?"已收藏":"收藏"));
//        menuItems.add(new MenuItem(R.drawable.ic_pop_share, "分享"));
//        if (type == TYPE_HIS){
//            setTitle("通知详情");
//        }
        menuItems.add(new MenuItem(R.drawable.ic_pop_red, "阅读名单"));
        mTopRightMenu.setAnimationStyle(R.style.TRM_ANIM_STYLE)  //默认为R.style.TRM_ANIM_STYLE
                .addMenuList(menuItems)
                .setOnMenuItemClickListener(position -> {
                    switch (position) {
                        case 0:
                            HttpHeper.get().userService().addCollect(id,1)
                                    .compose(getThread())
                                    .compose(bindToLifecycle())
                                    .subscribe(new CommonCallBack<String>(true, NoticeDetailActivity.this) {
                                        @Override
                                        public void onCallBackSuccess(String data) {
                                            LoadSuccessAndFailDialog.showSuccess(NoticeDetailActivity.this,data);
                                            detailEntity.setState(detailEntity.isCollection()?2:1);
                                        }
                                    });
                            break;
                        case 1:
                            NoticeReadDetailActivity__JumpCenter.builder(NoticeDetailActivity.this)
                                    .setNoRead(detailEntity.getNoRead())
                                    .setRead(detailEntity.getRead())
                                    .setId(id)
                                    .setIsCanTx(isCanTx)
                                    .create().go();
                            break;
                        case 2:
                            break;
                    }
                })
                .showAsDropDown(getImgRight(), -50, 50);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }

}
